home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / GrowBoxDock / Sources / Clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-30  |  11.7 KB  |  420 lines

  1. /*
  2.     File:        Clipboard.c
  3.  
  4.     Contains:    Clipboard support for simple text application.
  5.  
  6.     Version:    Mac OS X
  7.  
  8.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  9.                 ("Apple") in consideration of your agreement to the following terms, and your
  10.                 use, installation, modification or redistribution of this Apple software
  11.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  12.                 please do not use, install, modify or redistribute this Apple software.
  13.  
  14.                 In consideration of your agreement to abide by the following terms, and subject
  15.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  16.                 copyrights in this original Apple software (the "Apple Software"), to use,
  17.                 reproduce, modify and redistribute the Apple Software, with or without
  18.                 modifications, in source and/or binary forms; provided that if you redistribute
  19.                 the Apple Software in its entirety and without modifications, you must retain
  20.                 this notice and the following text and disclaimers in all such redistributions of
  21.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  22.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  23.                 Apple Software without specific prior written permission from Apple.  Except as
  24.                 expressly stated in this notice, no other rights or licenses, express or implied,
  25.                 are granted by Apple herein, including but not limited to any patent rights that
  26.                 may be infringed by your derivative works or by other works in which the Apple
  27.                 Software may be incorporated.
  28.  
  29.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  30.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  31.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  33.                 COMBINATION WITH YOUR PRODUCTS.
  34.  
  35.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  36.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  37.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  39.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  40.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  41.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  
  43.     Copyright © 1993-2001 Apple Computer, Inc., All Rights Reserved
  44. */
  45.  
  46. #include "MacIncludes.h"
  47.  
  48. #include "Clipboard.h"
  49.  
  50. // --------------------------------------------------------------------------------------------------------------
  51.  
  52. static OSStatus ScrapHasFlavor (ScrapRef scrap, ScrapFlavorType sft, Boolean *hasIt)
  53. {
  54.     OSStatus err = noErr;
  55.  
  56.     ScrapFlavorFlags sff;
  57.  
  58.     err = GetScrapFlavorFlags (scrap,sft,&sff);
  59.  
  60.     if (!err)
  61.         *hasIt = true;
  62.     else if (err == scrapFlavorNotFoundErr)
  63.     {
  64.         *hasIt = false;
  65.         err = noErr;
  66.     }
  67.  
  68.     return err;
  69. }
  70.  
  71. static OSStatus GetScrapFlavorDataH (ScrapRef scrap, ScrapFlavorType sft, Handle *scrapHandle)
  72. {
  73.     OSStatus err = noErr;
  74.  
  75.     do
  76.     {
  77.         Size sfs;
  78.         err = GetScrapFlavorSize (scrap,sft,&sfs);
  79.         if (err) break;
  80.         ReserveMem (sfs);
  81.         err = MemError ( );
  82.         if (err) break;
  83.         *scrapHandle = NewHandle (sfs);
  84.         err = MemError ( );
  85.         if (err) break;
  86.  
  87.         do
  88.         {
  89.             HLock (*scrapHandle);
  90.             err = MemError ( );
  91.             if (err) break;
  92.             err = GetScrapFlavorData (scrap,sft,&sfs,**scrapHandle);
  93.             HUnlock (*scrapHandle);
  94.             if (err) break;
  95.             err = MemError ( );
  96.             if (err) break;
  97.         }
  98.         while (false);
  99.  
  100.         if (err)
  101.         {
  102.             DisposeHandle (*scrapHandle);
  103.             break;
  104.         }
  105.     }
  106.     while (false);
  107.  
  108.     return err;
  109. }
  110.  
  111. // --------------------------------------------------------------------------------------------------------------
  112. // OOP INTERFACE ROUTINES
  113. // --------------------------------------------------------------------------------------------------------------
  114. static OSStatus ClipboardUpdateWindow(WindowRef pWindow, WindowDataPtr pData)
  115. {
  116. #pragma unused (pData)
  117.  
  118.     OSStatus    anErr;
  119.     FontInfo    theInfo;
  120.     ResType        validScrapType = kScrapReservedFlavorType;
  121.     Rect        topAreaRect;
  122.     Rect        bounds;
  123.     RgnHandle    oldClip = NewRgn();
  124.     Rect        eraseRect;
  125.     ScrapRef     scrap;
  126.     
  127.     GetWindowPortBounds( pWindow, &eraseRect );
  128.     
  129.     // clear out any data that was there before
  130.     GetClip(oldClip);
  131.     EraseRect(GetWindowPortBounds(pWindow, &bounds));
  132.     
  133.     // setup for the drawing
  134.     TextFont(applFont);
  135.     TextSize(9);
  136.     GetFontInfo(&theInfo);
  137.  
  138.     // caclulate our area at the top to say what type of contents the scrap is
  139.         GetWindowPortBounds(pWindow, &topAreaRect);
  140.     topAreaRect.bottom = topAreaRect.top + theInfo.ascent + theInfo.descent + theInfo.leading * 2 + 2;
  141.  
  142.     if( gMachineInfo.haveAppearanceMgr )
  143.     {
  144.         // draw a Finder-style window header
  145.         Rect placardRect = topAreaRect;
  146.         
  147.         InsetRect( &placardRect, -1, -1 );
  148.         
  149.         DrawThemePlacard( &placardRect, IsWindowHilited(pWindow) ? kThemeStateActive : kThemeStateInactive );
  150.         eraseRect.top = placardRect.bottom + 1;
  151.     }
  152.     else
  153.     {
  154.         // draw two lines under the area to separate it from the rest of the window
  155.         MoveTo(topAreaRect.left, topAreaRect.bottom - 2);
  156.         Line(topAreaRect.right - topAreaRect.left, 0);
  157.         Move(0, 2);
  158.         Line(-(topAreaRect.right - topAreaRect.left), 0);
  159.     }
  160.  
  161.     EraseRect( &eraseRect );
  162.  
  163.     anErr = GetCurrentScrap (&scrap);
  164.     nrequire (anErr, bailScrap);
  165.     // remember the scrap -- if it changes, we do an update!
  166.     ((ClipboardDataPtr) pData)->scrapRef = scrap;
  167.  
  168.     // draw a string describing the contents
  169.     {
  170.         Str255        theString;
  171.         UInt32        sfic;
  172.         Boolean        hasFlavor;
  173.  
  174.         anErr = GetScrapFlavorCount (scrap,&sfic);
  175.         nrequire (anErr, bailScrap);
  176.  
  177.         if (sfic == 0)
  178.             GetIndString(theString, kClipboardStrings, iClipboardNone);
  179.         else
  180.         {
  181.             anErr = ScrapHasFlavor (scrap,'PICT',&hasFlavor);
  182.             nrequire (anErr, bailScrap);
  183.  
  184.             if (hasFlavor)
  185.             {
  186.                 GetIndString(theString, kClipboardStrings, iClipboardPICT);
  187.                 validScrapType = 'PICT';
  188.             }
  189.             else
  190.             {
  191.                 anErr = ScrapHasFlavor (scrap,'TEXT',&hasFlavor);
  192.                 nrequire (anErr, bailScrap);
  193.  
  194.                 if (!hasFlavor)
  195.                     GetIndString(theString, kClipboardStrings, iClipboardUnknown);
  196.                 else
  197.                 {
  198.                     GetIndString(theString, kClipboardStrings, iClipboardText);
  199.                     validScrapType = 'TEXT';
  200.                 }
  201.             }
  202.         }
  203.             
  204.         MoveTo(topAreaRect.left + 4, topAreaRect.bottom - 4);
  205.  
  206.         // draw in the correct placard text color ••• need to add deviceloop
  207.         if( gMachineInfo.haveAppearanceMgr )
  208.         {
  209.             ThemeTextColor    themeTextColor = kThemeTextColorPlacardActive;
  210.         
  211.             if( ! IsWindowHilited(pWindow) )
  212.             {
  213.                 themeTextColor = kThemeTextColorPlacardInactive;
  214.             }
  215.             
  216.             SetThemeTextColor( themeTextColor, 32, true );
  217.         }
  218.         
  219.         DrawString(theString);
  220.     }
  221.     
  222.     // calculate the part *not* in our top area    
  223.  
  224.     topAreaRect.top        = topAreaRect.bottom+1;
  225.     topAreaRect.bottom    = GetWindowPortBounds(pWindow, &bounds)->bottom;
  226.  
  227.     if (validScrapType != kScrapReservedFlavorType) // now, draw the contents if we have a legal type to use
  228.     {
  229.         Handle scrapHandle;
  230.  
  231.         anErr = GetScrapFlavorDataH (scrap,validScrapType,&scrapHandle);
  232.         nrequire (anErr, bailScrap);
  233.  
  234.         switch (validScrapType)
  235.         {
  236.             case 'PICT':
  237.  
  238.                 {
  239.                     Rect picFrame    = (** (PicHandle) scrapHandle).picFrame;
  240.                     Rect clipRect    = topAreaRect;
  241.  
  242.                     clipRect.right        -= 15;
  243.                     clipRect.bottom        -= 15;
  244.  
  245.                     ClipRect (&clipRect);
  246.  
  247.                     DrawPicture ((PicHandle) scrapHandle, &picFrame);
  248.                 }
  249.                 break;
  250.                 
  251.             case 'TEXT':
  252.  
  253.                 {
  254.                     Rect textFrame = topAreaRect;
  255.  
  256.                     textFrame.right        -= 15;
  257.                     textFrame.bottom    -= 15;
  258.  
  259.                     HLockHi (scrapHandle);
  260.                     TETextBox(*scrapHandle, GetHandleSize (scrapHandle), &textFrame, teJustLeft);
  261.                 }
  262.                 break;
  263.         }
  264.  
  265.         DisposeHandle (scrapHandle);
  266.     }
  267.  
  268. bailScrap:
  269.  
  270.     // finally draw the grow icon, but omit our top area rect from the drawing
  271.     ClipRect(&topAreaRect);
  272.     DrawGrowIcon(pWindow);
  273.     
  274.     SetClip(oldClip);
  275.     DisposeRgn(oldClip);
  276.  
  277.     return anErr;
  278.  
  279. } // ClipboardUpdateWindow
  280.  
  281.  
  282. // --------------------------------------------------------------------------------------------------------------
  283.  
  284. static Boolean    ClipboardFilterEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent)
  285. {    
  286.  
  287.     // Force an update on scrap changes during activate/deactivate.
  288.  
  289.     switch (pEvent->what)
  290.     {
  291.         case nullEvent:
  292.  
  293.             {
  294.                 ScrapRef scrap;
  295.  
  296.                 if (!GetCurrentScrap (&scrap) && scrap != ((ClipboardDataPtr)pData)->scrapRef)
  297.                 {
  298.                     Rect bounds;
  299.                     InvalWindowRect(pWindow, GetWindowPortBounds(pWindow, &bounds));
  300.                 }
  301.             }
  302.             break;
  303.             
  304.         case activateEvt:
  305.                 {
  306.                 Rect bounds;
  307.                 InvalWindowRect(pWindow, GetWindowPortBounds(pWindow, &bounds));
  308.                 }
  309.             break;
  310.         
  311.         // Follow the HI guidelines and hide the clipboard when we are suspended.
  312.     
  313.         case osEvt:
  314.             if (((pEvent->message >> 24) & 0x0FF) == suspendResumeMessage)
  315.                 {
  316.                 if((pEvent->message & resumeFlag)==0)    // suspending
  317.                     {
  318.                     HideWindow(pWindow);
  319.                     pWindow = FrontWindow();
  320.                     if (pWindow)
  321.                         HiliteWindow(pWindow, false);
  322.                     }
  323.                 else                                    // resuming
  324.                     ShowWindow(pWindow);
  325.                 }
  326.             break;
  327.             
  328.     } // switch(what)
  329.     
  330.     return false;
  331.     
  332. } // ClipboardFilterEvent
  333.  
  334.  
  335. // --------------------------------------------------------------------------------------------------------------
  336.  
  337. static OSStatus    ClipboardKeyEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent, Boolean isMotionKey)
  338. {    
  339.     #pragma unused(pWindow, pData, pEvent, isMotionKey)
  340.  
  341.     return noErr;
  342.  
  343. } // ClipboardKeyEvent
  344.  
  345.  
  346. // --------------------------------------------------------------------------------------------------------------
  347.  
  348. static OSStatus    ClipboardGetBalloon(WindowPtr pWindow, WindowDataPtr pData, 
  349.         Point *localMouse, short * returnedBalloonIndex, Rect *returnedRectangle)
  350. {
  351. #pragma unused (pWindow, pData, localMouse, returnedRectangle)
  352.  
  353.     *returnedBalloonIndex = iNoBalloon;
  354.     
  355.     return noErr;
  356.  
  357. } // ClipboardGetBalloon
  358.  
  359. // --------------------------------------------------------------------------------------------------------------
  360.  
  361. static OSStatus    ClipboardCloseWindow(WindowPtr pWindow, void* refCon)
  362. {    
  363. #pragma unused(pWindow,refCon)
  364.  
  365.     ChangeCommandName(cShowClipboard, kClipboardStrings, iClipboardShow);
  366.     return noErr;
  367.  
  368. } // ClipboardCloseWindow
  369.  
  370.  
  371. // --------------------------------------------------------------------------------------------------------------
  372.  
  373. static OSStatus    ClipboardMakeWindow(WindowPtr pWindow, WindowDataPtr pData)
  374. {
  375. #pragma unused (pWindow)
  376.     BitMap bitmap;
  377.  
  378.     pData->hasGrow                 = true;
  379.     pData->pFilterEvent         = (FilterEventProc)     ClipboardFilterEvent;
  380.     pData->pKeyEvent            = (KeyEventProc)         ClipboardKeyEvent;
  381.     pData->pGetBalloon            = (GetBalloonProc)         ClipboardGetBalloon;
  382.     pData->pUpdateWindow         = (UpdateWindowProc)    ClipboardUpdateWindow;
  383.     pData->pCloseWindow            = (CloseWindowProc)        ClipboardCloseWindow;
  384.     
  385.     GetQDGlobalsScreenBits(&bitmap);
  386.     pData->contentRect.right = pData->contentRect.left + 
  387.                     bitmap.bounds.right - 
  388.                     bitmap.bounds.left - 96;
  389.     pData->contentRect.bottom = pData->contentRect.top + 150;
  390.     MoveWindow(pWindow, bitmap.bounds.left + 4, 
  391.             bitmap.bounds.bottom - 154, false);
  392.     
  393.     ChangeCommandName(cShowClipboard, kClipboardStrings, iClipboardHide);
  394.  
  395.     return noErr;
  396.     
  397. } // ClipboardMakeWindow
  398.  
  399.  
  400. // --------------------------------------------------------------------------------------------------------------
  401.  
  402. OSStatus    ClipboardPreflightWindow(PreflightPtr pPreflightData)
  403. {    
  404.     pPreflightData->resourceID            = kClipboardWindowID;
  405.     pPreflightData->continueWithOpen     = true;
  406.     pPreflightData->makeProcPtr         = ClipboardMakeWindow;
  407.     pPreflightData->storageSize         = sizeof(ClipboardDataRecord);
  408.     
  409.     return noErr;
  410.     
  411. } // ClipboardPreflightWindow
  412.  
  413. // --------------------------------------------------------------------------------------------------------------
  414.  
  415. void ClipboardGetFileTypes(OSType * pFileTypes, OSType * pDocumentTypes, short * numTypes)
  416. {
  417. #pragma unused (pFileTypes, pDocumentTypes, numTypes)
  418.  
  419. } // ClipboardGetFileTypes
  420.